package com.huarun.common.util;

import com.github.qcloudsms.httpclient.HTTPException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.huarun.common.ResponseMessage;

import java.io.IOException;
import java.util.Random;

/**
 * @author dzm
 * @创建时间 2018-08-07 16:00
 * @Description 短信发送
 * 
 */
@Component
public class SMSUtil {
	private static final Logger logger = LoggerFactory.getLogger(SMSUtil.class);

	/***
	 * 
	 * @Description 发送单条短信(腾讯云接口)
	 * @param mobile
	 * @param content
	 * @return
	 */
	public static ResponseMessage sendSMS(String mobile, String content) {
		ResponseMessage rm = new ResponseMessage();
		try {
			SmsSingleSender ssender = new SmsSingleSender(Integer.valueOf(Constant.TENCENT_SMS_APPID),
					Constant.TENCENT_SMS_APPKEY);
			SmsSingleSenderResult result = ssender.send(0, "86", mobile, content, "", "");
			logger.info("发送短信验证码 手机号：{},发送内容：{},发送结果：{}", mobile, content, result.toString());
			if (result.result != Constant.ZERO) {
				rm.setCode(ResultCodeValue.R_ERROR);
				rm.setMessage(result.errMsg);
				return rm;
			}
		} catch (Exception e) {
			rm.setCode(ResultCodeValue.R_ERROR);
			rm.setMessage("发送短信异常");
		}
		return rm;
	}

	public static void main(String[] args) {
		SmsSingleSender ssender = new SmsSingleSender(1400120894,
				"ff5f04943e668bc37b1f6af8a88d28ba");
		String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
		String mobile = "15928085495";
		String content = "你的手机欠费了";

		try {
	//		String content = "你的手机欠费了";
			SmsSingleSenderResult result = ssender.send(0, "86", mobile, content, "", "");
			System.out.println(result.toString());
		} catch (HTTPException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/***
	 * 
	 * @Description 发送多条短信(腾讯云接口)
	 * @param mobiles
	 * @param content
	 * @return
	 */
	public static ResponseMessage sendQuerySMS(String[] mobiles, String content) {
		ResponseMessage rm = new ResponseMessage();
		try {
			SmsMultiSender ssender = new SmsMultiSender(Integer.valueOf(Constant.TENCENT_SMS_APPID),
					Constant.TENCENT_SMS_APPKEY);
			SmsMultiSenderResult result = ssender.send(0, "86", mobiles, content, "", "");
			logger.info("发送短信验证码 手机号：{},发送内容：{},发送结果：{}", mobiles, content, result.toString());
			if (result.result != Constant.ZERO) {
				rm.setCode(ResultCodeValue.R_ERROR);
				rm.setMessage(result.errMsg);
				return rm;
			}
		} catch (Exception e) {
			rm.setCode(ResultCodeValue.R_ERROR);
			rm.setMessage("发送短信异常");
		}
		return rm;
	}

}
